<?php

/**
 * Description of users_controller
 *
 * @author Youness.MONDIR-SC
 */

class UsersController extends AppController {

	var $name = 'Users';
        function  beforeFilter() {
            parent::beforeFilter();
            $this->Auth->autoRedirect = false;
            //$this->Auth->allow('*');
            $this->set('active',3);
        }
        function home(){
            $this->set('active',0);
        }
        function login(){
            
          if ( count($this->Session->read('Auth.User'))>0)
                {
                        $this->User->Group->id = $this->Auth->user('group_id');

                        $this->Session->write(
                                'Auth.User.group_name',
                                $this->User->Group->field('title')
                        );
                        $a=  $this->User->find('list',array(
                            "recursive"=>-1,
                            'conditions'=>array('group_id'=>1),
                            'fields'=>array('mail')
                        ));
                        //debug($a);
                        $this->admin=$a;
                        $this->Session->write(
                                'adminMail',
                                serialize($a)
                        );
                        $this->Session->setFlash( __('You are logged in!',true) );

                        $pass=$this->User->getUserPass($this->Auth->user('id'));
                        $defPass=$this->Auth->password('mot2pass');
                        if($pass==$defPass){
                            $this->Session->write('chPwd',1);                           
                        }else{
                        
                        }
                        $this->redirect($this->Auth->redirect());
                        
                }

        }
        function logout(){
            $this->Session->setFlash( __('You are logged Out!',true) );
            $this->redirect($this->Auth->logout());
        }


        
        function filterUsers($list){
            $conditions=array();$c=array();$result=array();
            if($this->data){
                $data=  $this->data['User'];
                (strlen($data['name'])>0)?$c['name Like']='%'.$data['name'].'%':$c['name Like']='%*%';
                (strlen($data['surname'])>0)?$c['surname Like']='%'.$data['surname'].'%':$c['surname Like']='%*%';
                (strlen($data['id_code'])>0)?$c['id_code Like']='%'.$data['id_code'].'%':$c['id_code Like']='%*%';
                (strlen($data['id_erp'])>0)?$c['id_erp Like']='%'.$data['id_erp'].'%':$c['id_erp Like']='%*%';
                (strlen($data['id_win'])>0)?$c['id_win Like']='%'.$data['id_win'].'%':$c['id_win Like']='%*%';
                (strlen($data['id_hr'])>0)?$c['id_hr Like']='%'.$data['id_hr'].'%':$c['id_hr Like']='%*%';
                (strlen($data['mail'])>0)?$c['mail Like']='%'.$data['mail'].'%':$c['mail Like']='%*%';
                (strlen($data['group_id'])>0)?$c['group_id']=$data['group_id']:$c['group_id Like']='%*%';
                $conditions['OR']=$c;
                $result=$this->User->find('list',array(
                    'fields'=>array('UsName'),
                    'conditions'=>$conditions
                ));
                
            }
            $this->set('list',$list);
            $this->set('result',$result);
            $this->set('groups',  $this->User->Group->find('list'));          
        }

        function showUser($idUser=null){
            if($idUser){
                $user= $this->User->find('first',array(
                   'conditions'=>array('User.id'=>$idUser)
                    
                ));
                $this->set('userData',$user);
            }
        }

        function addUser(){
            debug($this->data);
            if($this->data){
                //$this->User->save($this->data);
            }
        }
        
        function editUser($id){
            $formData['statuses']= $this->User->Status->find('list');
            $formData['hrreferents']= $this->User->find('list',array('fields'=>array('strName')));            
            $formData['users']=$this->User->find('list',array('fields'=>array('UsName')));
            $this->set('formData',$formData);
            $this->set('user',  $this->User->find('first',array('conditions'=>array('User.id'=>$id),'recursive'=>-1)));
        }
        

	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
                $this->set('groups',  $this->User->Group->find('list')); 
	}
    
     function filterUsersIndex()
    {        
        $conditions=array();$c=array();$result=array();
            if($this->data){
                $data=  $this->data['User'];
                if(strlen($data['name'])>0) $c['User.name Like']='%'.$data['name'].'%';
                if(strlen($data['surname'])>0) $c['User.surname Like']='%'.$data['surname'].'%';
                if(strlen($data['id_code'])>0) $c['User.id_code Like']='%'.$data['id_code'].'%';
                if(strlen($data['id_erp'])>0) $c['User.id_erp Like']='%'.$data['id_erp'].'%';
                if(strlen($data['id_win'])>0) $c['User.id_win Like']='%'.$data['id_win'].'%';
                if(strlen($data['id_hr'])>0) $c['User.id_hr Like']='%'.$data['id_hr'].'%';
                if(strlen($data['mail'])>0) $c['User.mail Like']='%'.$data['mail'].'%';
                if(strlen($data['group_id'])>0) $c['User.group_id']=$data['group_id'];
                if(!empty($c)) $conditions['OR']=$c;
                $this->paginate=array(
                    'conditions'=>$conditions
                );
                
            }
            $this->set('users', $this->paginate());            
        $this->set('groups',  $this->User->Group->find('list'));   
        
        $this->render('index');
    }

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		$backups = $this->User->Backup->find('list');
		$managers = $this->User->Manager->find('list');
		$hrreferents = $this->User->Hrreferent->find('list');
		$statuses = $this->User->Status->find('list');
		$jobfunctions = $this->User->Jobfunction->find('list');
		$securitygroups = $this->User->Securitygroup->find('list');
		$companies = $this->User->Company->find('list');
		$groups = $this->User->Group->find('list');
		$jobprofiles = $this->User->Jobprofile->find('list');
		$powerdelegations = $this->User->Powerdelegation->find('list');
		$this->set(compact('backups', 'managers', 'hrreferents', 'statuses', 'jobfunctions', 'securitygroups', 'companies', 'groups', 'jobprofiles', 'powerdelegations'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
            if ($this->data['User']['password_change']==0){
                unset($this->data['User']['password']);
            }
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$backups = $this->User->Backup->find('list');
		$managers = $this->User->Manager->find('list');
		$hrreferents = $this->User->Hrreferent->find('list');
		$statuses = $this->User->Status->find('list');
		$jobfunctions = $this->User->Jobfunction->find('list');
		$securitygroups = $this->User->Securitygroup->find('list');
		$companies = $this->User->Company->find('list');
		$groups = $this->User->Group->find('list');
		$jobprofiles = $this->User->Jobprofile->find('list');
		$powerdelegations = $this->User->Powerdelegation->find('list');
		$this->set(compact('backups', 'managers', 'hrreferents', 'statuses', 'jobfunctions', 'securitygroups', 'companies', 'groups', 'jobprofiles', 'powerdelegations'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

        function account(){
            $u=$this->User->find('all',array(
                'conditions'=>array(
                    'User.id'=>$this->Auth->user('id')
                    ))
                    );
            $this->set('user',  $u);
        }

        function changePassword(){

         if($this->data){
            $pass=$this->User->getUserPass($this->Auth->user('id'));
            $oldPass=$this->Auth->password($this->data['User']['oldPassword']);
            $newPass=$this->Auth->password($this->data['User']['newPassword']);
            $valPass=$this->Auth->password($this->data['User']['validatePassword']);

            if ($pass==$oldPass){
                if($pass==$newPass){
                    $this->Session->setFlash( __('you can\'t reuse the same password !',true) ,'flash_err');
                }else{
                    if($newPass==$valPass){
                        $this->Session->write('chPwd',0);
                        $data['User']['id']=$this->Auth->user('id');
                        $data['User']['password']=$newPass;
                        if($this->User->save($data)){
                            $this->Session->setFlash( __('password modified!',true) );
                        }
                        $this->redirect(array('controller'=>'users','action'=>'home'));
                            
                        
                    }else{
                        $this->Session->setFlash( __('confirmation is wrong !',true) ,'flash_err');
                    }
                }
            }else{
                 $this->Session->setFlash( __('your actual password is wrong !',true) ,'flash_err');
            }}
        }
        
        
        function test($id=null){
            $u=$this->User->find('all',array(
                'conditions'=>array(
                    'User.id'=>$id
                )
            ));
            $this->set('t',$u);
        }
}

?>
